package com.yunduansing.demo.service.impl;

import com.yunduansing.demo.entity.DictionaryCategoryEntity;
import com.yunduansing.demo.entity.DictionaryEntity;
import com.yunduansing.demo.exception.RecordExistsException;
import com.yunduansing.demo.exception.ServiceException;
import com.yunduansing.demo.mapper.DictionaryCategoryMapper;
import com.yunduansing.demo.model.dictionary.DictionaryCategoryTreeModel;
import com.yunduansing.demo.model.dictionary.DictionaryPageRequestModel;
import com.yunduansing.demo.service.DictionaryCategoryService;
import com.yunduansing.demo.service.DictionaryService;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 字典类型 服务实现类
 * </p>
 *
 * @author yunduansing
 * @since 2020-05-24
 */
@Service
public class DictionaryCategoryServiceImpl extends ServiceImpl<DictionaryCategoryMapper, DictionaryCategoryEntity> implements DictionaryCategoryService {
    @Autowired
    private DictionaryService dictionaryService;

    @Override
            public boolean create(DictionaryCategoryEntity entity) throws RecordExistsException {
                var exists = existsByNameOrCode(entity.getName(),entity.getCode());
                if (exists.stream().filter(x -> x.getName().contains(entity.getName())&&x.getPid()==entity.getPid()).count() > 0)
                    throw new RecordExistsException("相同父级下已存在名称为：" + entity.getName() + "的字典类型。");
                if (exists.stream().filter(x -> x.getCode().contains(entity.getCode())).count() > 0)
                    throw new RecordExistsException("已存在编码为：" + entity.getCode() + "的字典类型。");
                return save(entity);
            }
        
            @Override
            public boolean update(DictionaryCategoryEntity entity) throws RecordExistsException {
                var exists = existsByNameOrCode(entity.getName(),entity.getCode());
                if (exists.stream().filter(x -> x.getName().contains(entity.getName()) && x.getId() != entity.getId()&&x.getPid()==entity.getPid())
                        .count() > 0)
                    throw new RecordExistsException("相同父级下已存在名称为：" + entity.getName() + "的字典类型。");
                if (exists.stream().filter(x -> x.getCode().contains(entity.getCode())&&x.getId()!=entity.getId()).count() > 0)
                    throw new RecordExistsException("已存在编码为：" + entity.getCode() + "的字典类型。");
                return updateById(entity);
            }
        
            private List<DictionaryCategoryEntity> existsByNameOrCode(String name,String code) {
                var wrapper = new QueryWrapper<DictionaryCategoryEntity>();
                wrapper.like("name", name).or().like("code", code);
                return list(wrapper);
            }

            @Override
            public List<DictionaryCategoryTreeModel> getTree() {
                return generateTree(0,this.list());
            }

            private List<DictionaryCategoryTreeModel> generateTree(int pid,List<DictionaryCategoryEntity> entities){
                var data=entities.stream().filter(x->x.getPid()==pid).collect(Collectors.toList());
                Collections.sort(data);
                var list=new ArrayList<DictionaryCategoryTreeModel>();
                if(data.size()>0){
                    for(var item:data){
                        if(item.getStatus()){
                            list.add(new DictionaryCategoryTreeModel(item, generateTree(item.getId(),entities)));
                        }
                    }
                }
                return list;
            }

            @Override
            public boolean delete(List<Integer> ids) throws ServiceException {
                var dictionaryWrapper=new QueryWrapper<DictionaryEntity>();
                dictionaryWrapper.in("category_id", ids);
                var existsDicByCate=dictionaryService.count(dictionaryWrapper);
                if(existsDicByCate>0) throw new ServiceException("该分类下存在字典，不能删除。");
                return removeByIds(ids);
            }

}
